package com.example.gateway.ribbon;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.List;

/**
 * @ClassName HashRule
 * @Author wengjt
 * @Date 2021/4/713:22
 * @Description
 **/
public class ConsistencyRule extends AbstractLoadBalancerRule {
    private Logger log = LoggerFactory.getLogger(ConsistencyRule.class);

    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {

    }
    /**
     *默认选择存活列表的第二个服务
     *    
     * @Author wengjt
     * @Date 15:45 2021/4/12
     */
    public Server choose(ILoadBalancer lb, Object key) {
        List<Server> reachableServers = lb.getReachableServers();
        return reachableServers.get(0);
    }

    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }
}
